
const app = getApp()

Page({
  data: {
    activeggIndex: 0,
    ggList: [],
    kms:[],
    km:'科目',
    levels:[],
    level:'级别',
    gms:[],
    gm:'规模',
    page: 1,
    flag:0,
    none:'',
    question_list:[],
    keyword:""
  },

  onLoad: function () {
    var that = this;
    that.getQuestionTop();
  },

  onShow:function(){
    var that=this;
    setTimeout(() => {
      that.getQuestionList();
    }, 300);
  },

  getQuestionTop: function(){
    var that = this;
    wx.request({
      url: app.globalData.domain + 'Index/GetQuestionTop',
      method: "POST",
      data: {},
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        that.setData({
          ggList:res.data.adv_list,
          kms: res.data.kms,
          levels: res.data.levels,
          gms: res.data.gms
        })
      }
    })
  },

  keyword:function(e){
    console.log(e.detail.value);
    this.setData({
      keyword: e.detail.value
    })
  },

  search:function(e){
    var that = this;
    that.setData({
      flag:0,
      none:'',
      house_list:[],
      page:1
    })
    that.getQuestionList();
  },

  getQuestionList:function(){
    var that = this;
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 300,
    })
    wx.request({
      url: app.globalData.domain + 'Index/GetQuestionList',
      method: "POST",
      data: {
        km : that.data.km,
        level : that.data.level,
        gm : that.data.gm,
        keyword : that.data.keyword,
        page: that.data.page
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if(res.data.question_list.length>0){
          that.setData({ 
            question_list: that.data.question_list.concat(res.data.question_list),
            page: parseInt(that.data.page) + 1
          })
        }else{
          that.setData({
            none: '暂无更多题目',
            flag:1
          });
        }
        if (that.data.question_list.length == 0) {
          that.setData({
            none: '暂无题目'
          });
        }
      }
    })
  },

  kmChange: function(e) {
    var that = this;
    if(e.detail.value==0){
      that.setData({
        km: '科目'
      })
    }else{
      that.setData({
        km: that.data.kms[e.detail.value],
      })
    }
    that.setData({
      page: 1,
      flag:0,
      none:'',
      question_list:[]
    })
    that.getQuestionList();
  },

  levelChange: function(e) {
    var that = this;
    if(e.detail.value==0){
      that.setData({
        level: '级别'
      })
    }else{
      that.setData({
        level: that.data.levels[e.detail.value],
      })
    }
    that.setData({
      page: 1,
      flag:0,
      none:'',
      question_list:[]
    })
    that.getQuestionList();
  },

  gmChange: function(e) {
    var that = this;
    if(e.detail.value==0){
      that.setData({
        gm: '规模'
      })
    }else{
      that.setData({
        gm: that.data.gms[e.detail.value],
      })
    }
    that.setData({
      page: 1,
      flag:0,
      none:'',
      question_list:[]
    })
    that.getQuestionList();
  },

  changeggImg(e) {
    let that = this
    that.activeggIndex = e.detail.current
    this.setData({
      activeggIndex: e.detail.current
    })
  },

  go_question: function (e) {
    if (e.currentTarget.dataset.id != 0) {
      wx.navigateTo({
        url: '/pages/question/question?id=' + e.currentTarget.dataset.id
      })
    }
  },

  share_question: function (e) {
    var that = this;
    return {
      title: e.currentTarget.dataset.id,
      path: '/pages/question/question?id=' + e.currentTarget.dataset.id
    }
  },

  onReachBottom: function () {
    var that = this;
    if(that.data.flag==0){
      that.getQuestionList();
    }
  },

  onShareAppMessage: function (e) {
    var that = this;
    return {
      title: '练习分享',
      path: '/pages/question/index'
    }
  }
  
})
